import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Login')
// iimport Login from '@/components/Login'
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Home')
// import Home from '@/components/Home'
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Welcome')
// import Welcome from '@/components/Welcome'
const Users = () => import(/* webpackChunkName:"user" */ '@/components/user/Users')
// import Users from '@/components/user/Users'
const Rights = () => import(/* webpackChunkName:"power" */ '@/components/power/Rights')
// import Rights from '@/components/power/Rights'
const Roles = () => import(/* webpackChunkName:"power" */ '@/components/power/Roles')
// import Roles from '@/components/power/Roles'
const Cate = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Cate')
// import Cate from '@/components/goods/Cate'
const Params = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Params')
// import Params from '@/components/goods/Params'
const List = () => import(/* webpackChunkName:"goods" */ '@/components/goods/List')
// import List from '@/components/goods/List'
const Add = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Add')
// import Add from '@/components/goods/Add'
const Order = () => import(/* webpackChunkName:"order" */ '@/components/orders/Order')
// import Order from '@/components/orders/Order'
const Report = () => import(/* webpackChunkName:"report" */ '@/components/report/Report')
// import Report from '@/components/report/Report'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 添加路由守卫,通过判断有误token跳转页面
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
